﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Asd.MP.DomainModel;
using Asd.MP.Entity;
using Asd.MP.Utility;
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Contexts;

namespace MobilePlatform.Business
{

    public class SystemInfoBal : BasicBal
    {

        public List<V_SerialNoListDataSet.V_SERIALNOLISTRow> GetSerailNoList(int pageIndex, int pageSize, string mobile, string account, out int totalRows)
        {
            var dataSource = _systemInfoDataAccess.GetSerailNoList();
            var result = dataSource.Where(i =>
               (string.IsNullOrEmpty(account) || i.EMPACCOUNT.Contains(account))
                && (string.IsNullOrEmpty(mobile) || i.MOBILE.Contains(mobile)));

            totalRows = result.Count();
            return result.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();

        }

        public V_SerialNoListDataSet.V_SERIALNOLISTRow GetSerailBySn(string sn)
        {
            var serailDt = _systemInfoDataAccess.GetSerailNoBySn(sn);
            var oneRow = serailDt.FirstOrDefault();
            if (oneRow == null)
            {
                oneRow = serailDt.NewV_SERIALNOLISTRow();
                oneRow.SN = Guid.NewGuid().ToString();
                oneRow.STATUS = (int)SerialNoStatus.UnActive;
            }

            oneRow.EMPACCOUNT = oneRow.IsEMPACCOUNTNull() ? string.Empty : oneRow.EMPACCOUNT;
            oneRow.MOBILE = oneRow.IsMOBILENull() ? string.Empty : oneRow.MOBILE;
            oneRow.REMARK = oneRow.IsREMARKNull() ? string.Empty : oneRow.REMARK;

            return oneRow;
        }

        public List<object> GetSerailStatusList()
        {
            return EnumsHelper.GetEnumsToList(typeof(SerialNoStatus));
        }

        public int SaveSerail(SerialNoEntity serial)
        {
            int result = 1;
            var oneRow = this.GetSerailNoListByMobile(serial.Mobile).FirstOrDefault();
            if (oneRow != null && oneRow.SN != serial.Sn)
            {
                result = 2;

            }
            else
            {
                _systemInfoDataAccess.SaveSerail(serial);
                result = 1;
            }

            return result;

        }

        public V_SerialNoListDataSet.V_SERIALNOLISTDataTable GetSerailNoListByAccount(string account)
        {
            return _systemInfoDataAccess.GetSerailNoListByAccount(account);
        }

        public V_SerialNoListDataSet.V_SERIALNOLISTDataTable GetSerailNoListByMobile(string mobile)
        {
            return _systemInfoDataAccess.GetSerailNoListByMobile(mobile);
        }

        public int SaveSerail(string mobile, string account)
        {
            int result = 1;

            if (this.GetSerailNoListByMobile(mobile).Count < 1)
            {

                SerialNoEntity serila = new SerialNoEntity();
                serila.EMPAccount = account;
                serila.Mobile = mobile;
                serila.Remark = string.Empty;
                serila.Sn = Guid.NewGuid().ToString();
                serila.Status = (int)SerialNoStatus.UnActive;
                this.SaveSerail(serila);
                result = 1;
            }
            else
            {
                result = 2;
            }

            return result;
        }

        public void ModifySerialNoStatus(List<SerialNoEntity> serialList)
        {
            _systemInfoDataAccess.ModifySerialNoStatus(serialList);
        }
    }
}
